//
//  CategoryViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/24.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "CategoryViewController.h"
#import "CategoryCollectionCell.h"
#import "PlatformStyleViewController.h"
#import "CategoryModel.h"

@interface CategoryViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (nonatomic, strong) NSArray *category;
@property (nonatomic, strong) NSArray *type;
@property (nonatomic, strong) UIImageView *viewLine;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) NSString *name;  // 试

@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.tableFooterView=[UIView new];
    self.title = @"品类";
    self.view.backgroundColor = [UIColor whiteColor];
    self.name = @"连衣裙0";
    [self requestData:YES];
    [self requestData:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if ([self.myTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.myTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.myTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.myTableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)requestData:(BOOL)ifType {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken};
    [RequestData CategoryParameters:[UIUtil toJsonString:dic] url:ifType?@"/gckjapi/queryProductType":@"/gckjapi/queryProductStyle" withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            if (ifType) {
                self.category = jsonArray;
                [self.myTableView reloadData];
            } else {
                self.type = jsonArray;
                [self.myCollectionView reloadData];
            }
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.category.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = ((CategoryModel *)[self.category objectAtIndex:indexPath.row]).typeName;
    [cell.textLabel setTextColor:[UIUtil colorWithHexString:@"#5f5f5f"]];
    cell.backgroundColor = [UIUtil colorWithHexString:@"#f3f3f3"];
    if (self.indexPath == nil && indexPath.row == 0) {
        self.indexPath = indexPath;
        [cell.textLabel setTextColor:[UIUtil colorWithHexString:@"#ff962f"]];
        cell.backgroundColor = [UIUtil colorWithHexString:@"#ffffff"];
        self.viewLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"categorySelected"]];
        self.viewLine.frame = CGRectMake(0, 0, 2, 42);
        [cell addSubview:self.viewLine];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:self.indexPath];
    [cell.textLabel setTextColor:[UIUtil colorWithHexString:@"#5f5f5f"]];
    cell.backgroundColor = [UIUtil colorWithHexString:@"#f3f3f3"];
    [self.viewLine removeFromSuperview];
    self.indexPath = indexPath;
    UITableViewCell *selectedCell = [self.myTableView cellForRowAtIndexPath:self.indexPath];
    [selectedCell.textLabel setTextColor:[UIUtil colorWithHexString:@"#ff962f"]];
    selectedCell.backgroundColor = [UIUtil colorWithHexString:@"#ffffff"];
    [selectedCell addSubview:self.viewLine];
    [self requestData:NO];
    self.name = ((CategoryModel *)[self.category objectAtIndex:indexPath.row]).typeName;
    //    [self.myCollectionView reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.type.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *const categoryIdentifier = @"CategoryCollectionCell";
    [self.myCollectionView registerNib:[UINib nibWithNibName:@"CategoryCollectionCell" bundle:nil]
            forCellWithReuseIdentifier:categoryIdentifier];
    CategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:categoryIdentifier forIndexPath:indexPath];
//    CategoryModel *model = [self.type objectAtIndex:indexPath.row];
    cell.name = ((CategoryModel *)[self.type objectAtIndex:indexPath.row]).styleName;
    return cell;
}


#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PlatformStyleViewController *view = [[PlatformStyleViewController alloc] init];
    view.ifCategory = YES;
    view.titleName = ((CategoryModel *)[self.type objectAtIndex:indexPath.row]).styleName;
    view.type = self.name;
    view.style = ((CategoryModel *)[self.type objectAtIndex:indexPath.row]).styleName;
    [self.navigationController pushViewController:view animated:YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((SCREEN_WIDTH-75-40)/3, (SCREEN_WIDTH-75-40)/3 + 20);
}

@end
